<?php
class Silk_Type extends Silk_Model
{

    protected $_objects = null;
    protected $_type_metas = null;
    protected $_sites = null;
    protected $_templates = null;

    protected $_clear_templates = false;
    protected $_clear_metas = false;

    protected $_templates_updated = false;
    protected $_metas_updated = false;

    protected $_new_templates = null;
    protected $_new_metas = null;

    protected $_default_permissions = null;

    protected $_data = array(
        'type_id' => null,
        'slug' => null,
        'name' => null,
        'description' => null,
        'author' => null,
        'email' => null,
        'website' => null,
        'version' => null,
        'virtual' => null
        );

    public function __set($pAtt, $pVal){

	switch ($pAtt){
	    case 'meta':
		$this->setTypeMetas($pVal);
		break;
	    case 'actions':
		$this->setActions($pVal);
		break;
	    default:
		parent::__set($pAtt, $pVal);
	}
    }

    public function save($pValidate=true){

	if (!$this->slug)
	    $this->slug = to_url_friendly($this->name);

	parent::save($pValidate);

	if ($this->_clear_templates)
	    $this->gateway->clearTemplates($this);

	if ($this->_templates_updated){
	    $templates = $this->gateway->getTemplateCollection($this->_new_templates);

	    foreach ($templates as $action){
		$action->type_id = $this->type_id;
		$action->save();
	    }
	}

	if ($this->_clear_metas)
	    $this->gateway->clearMetas($this);

	if ($this->_metas_updated){
	    $metas = $this->gateway->getMetaCollection($this->_new_metas);

	    foreach ($metas as $meta){
		$meta->type_id = $this->type_id;
		$meta->save();
	    }
	}
    }

    public function setActions(array $pActions){
	Silk::debug('Silk_Type.setActions: actions=' . logobject($pActions));
	if ($this->_new_templates === null) $this->_new_templates = array();

	$names = $pActions['name'];
	$contents = $pActions['content'];

	foreach ($names as $i => $action){
	    if ($action)
		$this->_new_templates[] = array(
		    'name' => $action,
		    'content' => $contents[$i]
		);
	}

	$this->_clear_templates = true;
	$this->_templates_updated = true;
    }

    public function setTypeMetas(array $pMetas){
	Silk::debug('Silk_Type.setTypeMetas: metas=' . logobject($pMetas));
	if ($this->_new_metas === null) $this->_new_metas = array();

	$names = $pMetas['name'];
	$input_types = $pMetas['input_type'];
	$default_values = $pMetas['default_value'];
	$list_values = $pMetas['list_values'];
	$validators = $pMetas['validator'];

	foreach ($names as $i => $meta){
	    if ($meta)
		$this->_new_metas[] = array(
		    'name' => $meta,
		    'input_type' => $input_types[$i],
		    'default_value' => $default_values[$i],
		    'list_values' => $list_values[$i],
		    'validator' => $validators[$i]
		);
	    //if ($meta['name']) $this->_new_metas[] = $meta;
	}

	$this->_clear_metas = true;
	$this->_metas_updated = true;
    }

    public function getTemplate($pAction){
	return $this->gateway->getTemplate($this, $pAction);
    }

    public function render(Silk_Object $pObject, $pAction=null, array $pVars=array()){
	return $this->gateway->render($this, $pObject, $pAction, $pVars);
    }

    public function getModuleType(){
	return $this->gateway->getModuleType($this);
    }

    public function hasPermissions($pPrivilege, $pUser=null){
	return $this->gateway->hasPermission($this, $pPrivilege, $pUser);
    }

    public function getPermissions(){
	return $this->gateway->getPermissions($this);
    }

}

 ?>